
package kpython.backend.semantic;

import java.util.HashMap;
import java.util.Map;
import kpython.KPythonNode;

/**
 * Representa a tabela de s�mbolos
 * 
 * @author Dinis
 */
public class SymbolTable {

	/**
	 * Construtor
	 */
    public SymbolTable() {
        root = new Scope("");
        current = root;
    }

    /**
     * Entra num novo Scope e actualiza a vari�vel com o scope actual
     */
    public void enterScope(String id) {
        current = current.nextChild(id);
    }

    /**
     * Retorna ao scope anterior, e actualiza a vari�vel com o scope actual
     */
    public void exitScope() {
        current = current.getParent();
    }

    /**
     * Insere um record no scope actual
     * 
     * @param key
     * @param item
     */
    public void put(Symbol key, Record item) {
        current.put(key, item);
    }

    /**
     * ?
     * @param key
     * @return
     */
    public Record lookup(Symbol key) {
        return current.lookup(key);
    }
    
    public Record currentLookup(Symbol key) {
        return current.currentLookup(key);
    }

    /**
     * Imprime a tabela de simbolos
     */
    public void printTable() {
        System.out.println("Symbol table:");
        System.out.println("-----------------------------");
        root.printScope(0);
        System.out.println("-----------------------------");
    }

    /**
     * Reinicializa os scopes
     */
    public void resetTable() {
        root.resetScope();
    }
    
    public KPythonNode getFunctionDef(String key) {
        return tmpFunctionNodes.get(key);
    }
    
    
    public void putFunctionDef(String key, KPythonNode functionNode) {
        //if(tmpFunctionNodes.get(key) == null)
            tmpFunctionNodes.put(key, functionNode);
    }
    
    public Scope getScope(String id){
        return root.getScope(id);
    }
    
    public Scope getCurrentScope() {
        return this.current;
    }
    
    public void goToScope(Scope to) {
        this.current = to;
    }
    
    
    /**
     * Scope ra�z
     */
    private Scope root; 
    
    
    /**
     * Table com o registo das funcoes definidas
     */
    private Map<String, KPythonNode> tmpFunctionNodes = new HashMap<String, KPythonNode>();

    /**
     * Scope actuals
     */
    private Scope current; 
}
